import requests
import json

url_prefix_api = 'http://cloud/prod-api'
path_login = '/api/v1/auth/login'
username = 'admin'
password = '123123'
params = {'username': username, 'password': password}
# 登录获取Token
resp = requests.post(url_prefix_api + path_login, "{}", params=params)

if resp.status_code == 200:
    resp_data = json.loads(resp.content)
    accessToken = resp_data['data']['accessToken']
    tokenType = resp_data['data']['tokenType']
    print(f"accessToken:{accessToken}, tokenType:{tokenType}")
    headers = {'Content-type': 'application/json', 'Authorization': f"{tokenType} {accessToken}"}
    # 获取数据集信息
    path_task_dataset = '/api/task/data'
    task_id = 1 # 任务标识，从数据平台中检索得到
    data_id = 1 # 数据集标识，从数据平台中检索得到
    req_task_dataset = {'task_id': task_id, 'data_id': data_id, 'operation': 'get'}
    resp = requests.post(url_prefix_api+path_task_dataset, json.dumps(req_task_dataset), headers=headers)
    if resp.status_code == 200:
        resp_data = json.loads(resp.content)
        # 数据集表结构标识
        data_dict_id = resp_data['data']['data_dict_id']
        data_type_id = resp_data['data']['type_id']
        data_type_dict = {1:"数据表，数据在postgresql中", 2: '时序表，数据在时序数据库TDengine中', 3: '图片', 4: '音视频', 5: '文档', 6: '文件'}
        print(f"任务1,名称:{resp_data['data']['name']},数据表:task_{task_id}_data_{data_id}, 表结构标识: {data_dict_id}, "
              f"数据类型:{data_type_dict[data_type_id]}")
        path_task_datadict = '/api/data_dict'
        req = {"task_id": task_id, "data_dict_id": data_dict_id, "depth": 1, "operation": "get"}  # 依次为任务标识、数据字典标识（与数据集中data_dict_id对应）等

        resp = requests.post(url_prefix_api + path_task_datadict, json.dumps(req), headers=headers)
        if resp.status_code == 200:
            resp_data = json.loads(resp.content)
            datadict = resp_data['data']

            # 输出列名
            for i in datadict[0]['children']:
                if i['classifier'] == 'column':
                    print(f"列名: {i['name']}, 描述: {i['description']}")

else:
    print(resp.content)
